package com.woniu.web.controller.bedinfo;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.common.entity.TbJpBedinfo;
import com.woniu.resultSet.Result;
import com.woniu.resultSet.Status;
import com.woniu.web.service.TbJpBedInfoService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;

/**
 * @author jiaoxiuxiu
 * @date 2020/9/24 14:28
 */
@RestController
@RequestMapping("/bedinfo")
public class BedInfoController {
    @Resource
    TbJpBedInfoService tbJpBedInfoService;

    //按条件及分页进行查询
    @PostMapping("/findAll")
    public Result findAllByExample(@RequestBody BedinfoExample bedinfoExample){

        //获得Page
        Page page = bedinfoExample.getPage();
        //设置起始页
        PageHelper.startPage(page.getPageNum(),page.getPageSize());
        //按条件查询数据
        List list =tbJpBedInfoService.findBybedinfoExample(bedinfoExample);
        //将查询到的数据进行分页
        PageInfo pageInfo = new PageInfo(list);
        //将查询到的数据返回页面
        if (list.size()!=0){
            return new Result(Status.SUCCESS,"查询成功",pageInfo);
        }else{
            return new Result(Status.FAIL,"查询失败",pageInfo);
        }
    }

    //删除单个
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable Long id){
        Integer re = tbJpBedInfoService.deleteBed(id);
        if (re!=0){
            return new Result(Status.SUCCESS,"删除成功",null);
        }else {
            return new Result(Status.FAIL,"删除失败",null);
        }
    }

    //批量删除
    @PostMapping("/deleteList")
    public Result DeleteByArray(@RequestBody Long [] ids){
        Integer re = tbJpBedInfoService.DeleteByArray(ids);
        if (re!=0){
            return new Result(Status.SUCCESS,"删除成功",null);
        }else {
            return new Result(Status.FAIL,"删除失败",null);
        }
    }

    //审核状态更改
    @PutMapping("/changeBgrStatus/{id}")
    public Result updateBgrStatus(@PathVariable Long id){
        System.out.println(id);
        Integer re = tbJpBedInfoService.updateBgrStatus(id);
        if (re!=0){
            return new Result(Status.SUCCESS,"审核成功",null);
        }else {
            return new Result(Status.FAIL,"审核失败",null);
        }
    }

    //关机
    @PutMapping("/shutdown/{id}")
    public Result shutdown(@PathVariable Long id){
        Integer re = tbJpBedInfoService.updateStatus(id);
        if (re!=0){
            return new Result(Status.SUCCESS,"关机成功",null);
        }else {
            return new Result(Status.FAIL,"关机失败",null);
        }
    }

    //开机
    @PutMapping("/bootup/{id}")
    public Result bootup(@PathVariable Long id){
        Integer re = tbJpBedInfoService.bootupStatus(id);
        if (re!=0){
            return new Result(Status.SUCCESS,"关机成功",null);
        }else {
            return new Result(Status.FAIL,"关机失败",null);
        }
    }

    //查询单个
    @PostMapping("/findone/{id}")
    public Result findOne(@PathVariable Long id){
        TbJpBedinfo bedinfo = tbJpBedInfoService.findOne(id);
        if (bedinfo!=null){
            return new Result(Status.SUCCESS,"查询成功",bedinfo);
        }else {
            return new Result(Status.FAIL,"查询失败",null);
        }
    }
}
